package com.mentaleye.biz.impl;

import java.util.List;
import java.util.Map;

import com.mentaleye.bean.StudentsBean;
import com.mentaleye.util.CommonResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.OaClassRelationBean;
import com.mentaleye.biz.OaClassRelationBiz;
import com.mentaleye.dal.model.OaClassRelationExample;
import com.mentaleye.dal.model.OaClassRelationExample.Criteria;
import com.mentaleye.dal.model.OaClassRelationKey;
import com.mentaleye.service.OaClassRelationService;

@Service
public class OaClassRelationBizImpl implements OaClassRelationBiz{

	private static Logger logger = Logger.getLogger(OaClassRelationBizImpl.class);
	
	@Autowired private OaClassRelationService oaClassRelationService;
	
	@Override
	public boolean addOaClassRelation(OaClassRelationBean oaClassRelationBean) {
		try {
			if(oaClassRelationBean != null){
				if(oaClassRelationBean.getPersonType()==23){
					OaClassRelationBean oaClassRelation = oaClassRelationService.queryOaClassRelationByKey(oaClassRelationBean.getOaclassId(), oaClassRelationBean.getPersonId());
					if(oaClassRelation==null){
						return oaClassRelationService.addOaClassRelation(oaClassRelationBean);
					}else{
						return oaClassRelationService.updateOaClassRelation(oaClassRelationBean);
					}
				}else{
					OaClassRelationExample example = new OaClassRelationExample();
					Criteria criteria = example.createCriteria();
					criteria.andOaclassIdEqualTo(oaClassRelationBean.getOaclassId());
					criteria.andPersonRoleEqualTo(oaClassRelationBean.getPersonRole());
					List<OaClassRelationBean> oaClassRelation = oaClassRelationService.queryOaclassRelation(example);
					if(oaClassRelation==null || oaClassRelation.size()<=0){
						return oaClassRelationService.addOaClassRelation(oaClassRelationBean);
					}else{
						boolean result = false;
						for (OaClassRelationBean oaClassRelationBean2 : oaClassRelation) {
							OaClassRelationKey key = new OaClassRelationKey();
							key.setOaclassId(oaClassRelationBean2.getOaclassId());
							key.setPersonId(oaClassRelationBean2.getPersonId());
							result = oaClassRelationService.removeOaClassRelation(key);
						}
						if(result){
							return oaClassRelationService.addOaClassRelation(oaClassRelationBean);
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("编辑班级人员异常",e);
		}
		return false;
	}
	
	@Override
	public boolean addOaClassRelationBeanList(List<OaClassRelationBean> list){
		return oaClassRelationService.addOaClassRelationList(list);
	}

	@Override
	public CommonResult updateOaClassRelation(OaClassRelationBean oaClassRelationBean) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if(oaClassRelationBean != null){
				success = oaClassRelationService.updateOaClassRelation(oaClassRelationBean);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("修改班级人员异常",e);
		}
		return result;
	}
	
	@Override
	public CommonResult updateOaClassRelationList(OaClassRelationBean oaClassRelationBean, List<Integer> personIdList) {
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			if(oaClassRelationBean != null && personIdList.size()>0){
				success =  oaClassRelationService.updateOaClassRelationList(oaClassRelationBean, personIdList);
			}
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("批量修改班级人员异常",e);
		}
		return result;
	}

	public CommonResult removeByPersonIdList(List<Integer> personIdList){
		CommonResult result = new CommonResult();
		boolean success = false;
		try {
			success = oaClassRelationService.deleteByPersonIdList(personIdList);
			result.setIsSuccess(success);
		} catch (Exception e) {
			logger.error("批量修改班级人员异常",e);
		}
		return result;
	}
	
	@Override
	public List<OaClassRelationBean> queryOaClassRelationById(List<Integer> ids){
		try {
			if(ids.size() >= 0){
				return oaClassRelationService.queryOaClassRelationById(ids);
			}
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}
	
	@Override
	public List<OaClassRelationBean> queryOaClassRelationByPersonId(List<Integer> ids){
		try {
			if(ids.size() >= 0){
				return oaClassRelationService.queryOaClassRelationByPersonId(ids);
			}
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}

	public List<StudentsBean> queryNotClassStudent(){
		try {
			return oaClassRelationService.queryNotClassStudent();
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}

	@Override
	public OaClassRelationBean queryOaClassRelationByKey(Integer oaclassId, Integer personId) {
		try {
			if(oaclassId!=null && personId != null){
				return oaClassRelationService.queryOaClassRelationByKey(oaclassId, personId);
			}
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}

	@Override
	public List<OaClassRelationBean> queryOaClassRelationByIdAndPersonType(Integer oaclassId, Integer personType) {
		try {
			if(oaclassId!=null && personType!=null){
				OaClassRelationBean classRelationBean = new OaClassRelationBean();
				classRelationBean.setOaclassId(oaclassId);
				classRelationBean.setPersonType(personType);
				return oaClassRelationService.queryOaClassRelationByIdAndPersonType(classRelationBean);
			}
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}
	
	@Override
	public OaClassRelationBean queryOaClassIdByPersonId(int personId){
		try {
			return oaClassRelationService.queryClassIdByPersonId(personId);
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}
	
	@Override
	public List<Map<String, Object>> queryClassCount(OaClassRelationBean oaClassRelationBean) {
		try {
			return oaClassRelationService.queryClassCount(oaClassRelationBean);
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}
	
	public List<OaClassRelationBean> queryOaclassRelation(OaClassRelationExample example){
		try {
			return oaClassRelationService.queryOaclassRelation(example);
		} catch (Exception e) {
			logger.error("查询班级人员异常",e);
		}
		return null;
	}

	@Override
	public List<OaClassRelationBean> queryStudentByClassId(Integer oaclassId) {
		try {
			return oaClassRelationService.queryStudentByClassId(oaclassId);
		} catch (Exception e) {
			logger.error("根据班级查询学生异常",e);
			return null;
		}
	}

	@Override
	public CommonResult deleteBySystem(Integer personId, Integer userType) {
		CommonResult result=new CommonResult();
		try {
			if(this.oaClassRelationService.deleteBySystem(personId, userType))
			{
				result.setIsSuccess(true);
				result.setResultMsg("删除成功");
			}
			else
			{
				result.setResultMsg("删除失败");
			}
		} catch (Exception e) {
			logger.error("删除班级关联学生信息异常",e);
			result.setResultMsg("删除班级关联学生信息异常");
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getListByMap(Map<String, Object> map) {
		List<Map<String, Object>> mapList=null;
		try {
			 mapList=this.oaClassRelationService.getListByMap(map);
		} catch (Exception e) {
			logger.error("根据班级查询学生异常",e);
		}
		return mapList;
	}

	@Override
	public Map<String, Object> getByMap(Map<String, Object> map) {
		Map<String, Object> resultMap=null;
		try {
			List<Map<String, Object>> mapList = this.oaClassRelationService.getListByMap(map);
			if(!mapList.isEmpty()){
				resultMap=mapList.get(0);
			}
		} catch (Exception e) {
			logger.error("根据班级查询学生异常",e);
		}
		return resultMap;
	}


}
